var n = 0, flag = true, JSONData = "";
var appdata = "", account = "", num = 0, total = 0, pagelen = 0, upl = " ", activ = " ", pltxt = "", pltutxt = "",
    pltutxt1 = "", imgfun = "";
var plxx = new Array();
var pltu = new Array();
var newsbox = $(".datalist");
var showtu = $(".showbigtu");
/*初始化*/
var counter = 1;
/*计数器*/
var pageStart = 0;
/*offset*/
var pageSize = 1;
/*size*/

/*首次加载*/
getData(pageStart, pageSize);

/*监听加载更多*/
$(document).on('click', '.js-load-more', function () {
    counter++;
    pageStart = counter * pageSize;

    getData(pageStart, pageSize);
});

function getData(offset, size) {
    $.ajax({
        url: "pllist",
        type: "POST",
        dataType: "JSON",
        data: {"form": counter, "merid": $(".merid").val()},
        success: function (data) {
            $(".merid").val(data.merid);

            /*使用for循环模拟SQL里的limit(offset,size)*/
            console.log(data)
            var Comm = data.Page.pageList;

            for (var o = 0; o < Comm.length; o++) {
                upl = "";
                for (var pf = 0; pf < Comm[o].commentsScore; pf++) {
                    upl += "<span></span>";
                }
                //判断用户头像
                if (Comm[o].useActiv != "") {
                    activ = Comm[o].useActiv;
                } else {
                    activ = "/images/default_avatar.jpg"
                }

                //获得用户的选项
                if (Comm[o].commentsOption != "") {
                    plxx = Comm[o].commentsOption.split(",")
                    pltxt = ""
                    for (var pli = 0; pli < plxx.length; pli++) {
                        pltxt += " <label style='font-size:16px;border: 2px solid #00c3ad;'>" + plxx[pli] + "</label> ";
                    }
                }

                //获得用户的选项
                if (Comm[o].commentsTu != "") {
                    pltu = Comm[o].commentsTu.split(",")
                    pltutxt = ""
                    pltutxt1 = ""
                    for (var ptu = 0; ptu < pltu.length; ptu++) {

                        //代表是最后一项
                        if (ptu == pltu.length - 1) {
                            pltutxt1 += "<li class='img'><img class='imgclick' src='/" + pltu[ptu] + "' > <div class='tip'><img src='/images/tip-img.jpg' ><span>" + pltu.length + "</span> </div></li>";
                        } else {
                            pltutxt += "<li class='img'><img class='imgclick' src='/" + pltu[ptu] + "'></li>";
                        }

                    }
                }

                appdata += "<ul><li class='item'><img class='user' src='/ " + activ + " '></img> <div class='conts'><div class='name am-cf'><div class='left'>" + Comm[o].useName + "<i class='vip'></i></div><span class='date'>" + Comm[o].commentsDate + "</span></div><div class='pingfen am-cf'>" + upl + "</div><div class='pingfen am-cf'>" + pltxt + "</div><div class='comment-text'  style='margin: 10px;'><p>" + Comm[o].commentsText + "</p></div><div class='comment-img'><ul class='am-cf' thumblist='list1'>" + pltutxt + "" + pltutxt1 + "</ul></div></ul> "
                newsbox.html("")
                newsbox.append(appdata);

                //点击图片放大查看
                $.fn.ImgZoomIn = function () {
                    var window_h = $(window).height();
                    var scroll_h = $(window).scrollTop();

                    bgstr = '<div id="ImgZoomInBG" style="position: absolute;filter:Alpha(Opacity=70); opacity:0.7;z-index: 10000;background-color: #000;display: none;"></div>';
                    imgstr = '<img id="ImgZoomInImage" src="' + $(this).attr('src') + '" style="cursor:pointer; display:none; position:absolute; z-index:10001;" />';
                    if ($('#ImgZoomInBG').length < 1) {
                        showtu.append(bgstr);
                    }
                    if ($('#ImgZoomInImage').length < 1) {
                        showtu.append(imgstr);
                    }
                    else {
                        $('#ImgZoomInImage').attr('src', $(this).attr('src'));
                    }

                    $('#ImgZoomInBG').css('top', scroll_h + 'px');
                    $('#ImgZoomInBG').css('width', '100%');
                    $('#ImgZoomInBG').css('height', window_h + 'px');

                    $('#ImgZoomInImage').css('width', '100%');
                    $('#ImgZoomInImage').css('height', (window_h / 2) + 'px');
                    $('#ImgZoomInImage').css('top', (scroll_h + window_h / 4) + 'px');

                    $('#ImgZoomInBG').show();
                    $('#ImgZoomInImage').show();
                };

                // PC端
                $(document).ready(function () {
                    $(".imgclick").bind("click", function () {
                        $(this).ImgZoomIn();
                        $(document.body).css({
                            "overflow-x": "hidden",
                            "overflow-y": "hidden"
                        });
                    });
                    $("#ImgZoomInImage").bind("click", function () {
                        $('#ImgZoomInImage').hide();
                        $('#ImgZoomInBG').hide();
                        $(document.body).css({
                            "overflow-x": "auto",
                            "overflow-y": "auto"
                        });
                    });
                });
                // 手机端
                $(document).ready(function () {
                    $(".imgclick").bind("click", function (t) {
                        $(this).ImgZoomIn();
                        document.ontouchstart = function () {
                            return false;
                        }
                        t.preventDefault();
                    });

                    $(document).on('touchend', '#ImgZoomInImage', function (t) {
                        $('#ImgZoomInImage').hide();
                        $('#ImgZoomInBG').hide();
                        document.ontouchstart = function () {
                            return true;
                        }
                        t.preventDefault();
                    });
                });

            }

            if (Comm.length == 0) {
                $(".userpl").html("")
                appdata = "<img style='position: relative;top: 90px;left: -60px;' src='/images/kong.png'>";
                newsbox.html("")
                newsbox.append(appdata);
            }

            /*隐藏more按钮*/
            if (data.Page.hasNextPage == false) {
                $(".js-load-more").hide();
            } else {
                $(".js-load-more").show();
            }
        },
        error: function (xhr, type) {

        }
    });
}

$(".back img").click(function () {

    location.href = "doindex"
})
